/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-08-25
 * 9:30
 */
public class Test {
}
class Solution {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null){
            return root;
        }
        if(root==p||root==q){
            return root;
        }
        TreeNode leftT=lowestCommonAncestor(root.left,p,q);
        TreeNode rightT=lowestCommonAncestor(root.right,p,q);
        if(leftT!=null&&rightT!=null){
            return root;
        }else if(leftT!=null){
            return leftT;
        }else if(rightT!=null){
            return rightT;
        }
        return null;
    }
}
